/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmium;

import java.lang.reflect.Method;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.FalseExit;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides primitives related to {@link java.lang.Object}.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Objects {

    /**
     * No instance of this class.
     */
    private Objects() {
    } // end empty constructor

    /**
     * Constructs a new object.
     * @param ctxt context
     * @param unit ignored
     * @return new object
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_make(final CodeRunner ctxt,
                                            final Value unit)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(new Object());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_make(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.Object#clone()} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_clone(final CodeRunner ctxt,
                                             final Value obj)
        throws Fail.Exception {
        try {
            final Object o = obj.asBlock().asCustom();
            final Method m = o.getClass().getMethod("clone");
            return Cadmium.createObject(m.invoke(o));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_clone(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.Object#equals(java.lang.Object)} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @param obj2 parameter for method execution
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_equals(final CodeRunner ctxt,
                                              final Value obj,
                                              final Value obj2)
        throws Fail.Exception {
        try {
            return obj.asBlock().asCustom().equals(obj2.asBlock().asCustom())
                ? Value.TRUE
                : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_equals(CodeRunner, Value, Value)'

    /**
     * Executes {@link java.lang.Object#getClass()} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_get_class(final CodeRunner ctxt,
                                                 final Value obj)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(obj.asBlock().asCustom().getClass());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_get_class(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.Object#hashCode()} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_hash_code(final CodeRunner ctxt,
                                                 final Value obj)
        throws Fail.Exception {
        try {
            final Block b = Block.createCustom(Custom.INT_32_SIZE,
                                               Custom.INT_32_OPS);
            b.setInt32(obj.asBlock().asCustom().hashCode());
            return Value.createFromBlock(b);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_hash_code(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.Object#notify()} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @return <i>unit</i>
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_notify(final CodeRunner ctxt,
                                              final Value obj)
        throws Fail.Exception {
        try {
            final Object o = obj.asBlock().asCustom();
            synchronized (o) {
                o.notify();
            } // end synchronized
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_notify(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.Object#notifyAll()} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @return <i>unit</i>
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_notify_all(final CodeRunner ctxt,
                                                  final Value obj)
        throws Fail.Exception {
        try {
            final Object o = obj.asBlock().asCustom();
            synchronized (o) {
                o.notifyAll();
            } // end synchronized
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_notify_all(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.Object#toString()} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_to_string(final CodeRunner ctxt,
                                                 final Value obj)
        throws Fail.Exception {
        try {
            return Value.createFromBlock(Block.createString(obj.asBlock().asCustom().toString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_to_string(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.Object#wait()} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @return <i>unit</i>
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_wait(final CodeRunner ctxt,
                                            final Value obj)
        throws Fail.Exception, FalseExit {
        try {
            final Object o = obj.asBlock().asCustom();
            synchronized (o) {
                try {
                    o.wait();
                } catch (final InterruptedException ie) {
                    final FalseExit fe =
                        FalseExit.createFromContext(ctxt.getContext());
                    fe.fillInStackTrace();
                    throw fe;
                } // end try/catch
            } // end synchronized
            return Value.UNIT;
        } catch (final FalseExit sfe) {
            throw sfe;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_wait(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.Object#wait(long)} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @param time parameter for method execution
     * @return <i>unit</i>
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_wait_time1(final CodeRunner ctxt,
                                                  final Value obj,
                                                  final Value time)
        throws Fail.Exception, FalseExit {
        try {
            final Object o = obj.asBlock().asCustom();
            final long timeout = time.asBlock().asInt64();
            synchronized (o) {
                try {
                    o.wait(timeout);
                } catch (final InterruptedException ie) {
                    final FalseExit fe =
                        FalseExit.createFromContext(ctxt.getContext());
                    fe.fillInStackTrace();
                    throw fe;
                } // end try/catch
            } // end synchronized
            return Value.UNIT;
        } catch (final FalseExit sfe) {
            throw sfe;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_wait_time1(CodeRunner, Value, Value)'

    /**
     * Executes {@link java.lang.Object#wait(long, int)} method.
     * @param ctxt context
     * @param obj object to execute method on
     * @param time parameter for method execution
     * @param nano parameter for method execution
     * @return <i>unit</i>
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_object_wait_time2(final CodeRunner ctxt,
                                                  final Value obj,
                                                  final Value time,
                                                  final Value nano)
        throws Fail.Exception, FalseExit {
        try {
            final Object o = obj.asBlock().asCustom();
            final long timeout = time.asBlock().asInt64();
            final int nanos = nano.asBlock().asInt32();
            synchronized (o) {
                try {
                    o.wait(timeout, nanos);
                } catch (final InterruptedException ie) {
                    final FalseExit fe =
                        FalseExit.createFromContext(ctxt.getContext());
                    fe.fillInStackTrace();
                    throw fe;
                } // end try/catch
            } // end synchronized
            return Value.UNIT;
        } catch (final FalseExit sfe) {
            throw sfe;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_object_wait_time2(CodeRunner, Value, Value, Value)'

} // end class 'Objects'
